package com.repair.system.controller;

import com.repair.system.entity.FaultRecord;
import com.repair.system.service.FaultRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/faults")
@RequiredArgsConstructor
public class FaultRecordController {
    private final FaultRecordService faultRecordService;
    
    @GetMapping
    public String listFaults(Model model) {
        model.addAttribute("faults", faultRecordService.getAllFaults());
        return "fault/list";
    }
    
    @GetMapping("/new")
    public String showCreateForm(Model model) {
        model.addAttribute("fault", new FaultRecord());
        return "fault/create";
    }
    
    @PostMapping("/save")
    public String saveFault(@ModelAttribute FaultRecord fault) {
        faultRecordService.saveFault(fault);
        return "redirect:/faults";
    }
    
    @GetMapping("/{id}")
    public String viewFault(@PathVariable Long id, Model model) {
        model.addAttribute("fault", faultRecordService.getFaultById(id));
        return "fault/detail";
    }
}
